<?php
namespace app\vueapi\controller;

use app\common\model\{
    Rate as RateModel, Laboratory as LaboratoryModel, Semester as SemesterModel, Week as WeekModel
};

use think\Db;

class Rate extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $page = $data['pageSize'] ?? 10;
        $Laboratory = LaboratoryModel::find()->paginate($page);
        $res = $Laboratory;
        $res1 = [];
        foreach($Laboratory as $key => $value){
            $laboratory_id['laboratory_id'] = $value['id'];
            for($i = 1; $i <= 20; $i++) {
                $field = 'semester_'.$i;
                $field_number = 'semester_number_'.$i;
                $res[$key][$field] = 0;
                $semester = SemesterModel::where($laboratory_id)->field($field)->find();
                if(!empty($semester[$field])) {
                    $day['id'] = $semester[$field];
                    $week = WeekModel::where($day)->find();
                    if(!empty($week['mon'])) {
                        $week_day = json_decode($week['mon']);
                        for($j = 0; $j < 9; $j++) {
                            if(!empty($week_day[$j])) {
                                $res[$key][$field] += 1;
                            }
                        }
                    }
                    if(!empty($week['tue'])) {
                        $week_day = json_decode($week['tue']);
                        for($j = 0; $j < 9; $j++) {
                            if(!empty($week_day[$j])) {
                                $res[$key][$field] += 1;
                            }
                        }
                    }
                    if(!empty($week['wed'])) {
                        $week_day = json_decode($week['wed']);
                        for($j = 0; $j < 9; $j++) {
                            if(!empty($week_day[$j])) {
                                $res[$key][$field] += 1;
                            }
                        }
                    }
                    if(!empty($week['thu'])) {
                        $week_day = json_decode($week['thu']);
                        for($j = 0; $j < 9; $j++) {
                            if(!empty($week_day[$j])) {
                                $res[$key][$field] += 1;
                            }
                        }
                    }
                    if(!empty($week['fri'])) {
                        $week_day = json_decode($week['fri']);
                        for($j = 0; $j < 9; $j++) {
                            if(!empty($week_day[$j])) {
                                $res[$key][$field] += 1;
                            }
                        }
                    }
                    if(!empty($week['sat'])) {
                        $week_day = json_decode($week['sat']);
                        for($j = 0; $j < 9; $j++) {
                            if(!empty($week_day[$j])) {
                                $res[$key][$field] += 1;
                            }
                        }
                    }
                    if(!empty($week['sun'])) {
                        $week_day = json_decode($week['sun']);
                        for($j = 0; $j < 9; $j++) {
                            if(!empty($week_day[$j])) {
                                $res[$key][$field] += 1;
                            }
                        }
                    }
                    $res[$key][$field_number] = round($res[$key][$field]/63*100,2)."%";
                }else {
                    $res[$key][$field_number] = '0%';
                }
                $res1[$key]['name'] = $res[$key]['name'];
                $res1[$key]['week'][$i]['semester'] = $res[$key][$field_number];
                $res1[$key]['week'][$i]['semester_number'] = $res[$key][$field];
                $res['data1'] = $res1;
            }
        };
        return json(msg(1, $res));
    }
}
?>